package com.zm.demo.netty.server.count;


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

public class CountServer {

    public static void main(String[] args) throws Exception{
        new CountServer().start();
    }


    public void start() throws Exception{
        ServerBootstrap server = new ServerBootstrap();
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        final CountServerHander countServerHander = new CountServerHander();
        server.group(eventLoopGroup)
                .channel(NioServerSocketChannel.class)
                .localAddress(new InetSocketAddress(9999))
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(countServerHander);
                    }
                });
        try {
            ChannelFuture channelFuture = server.bind().sync();
            channelFuture.channel().closeFuture().sync();
//        } catch (Exception e){
//            e.printStackTrace();
        }finally {
            eventLoopGroup.shutdownGracefully().sync();
        }
    }

}
